package mumethod;

//import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

//import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
//import net.sf.jasperreports.engine.JasperPrint;
//import net.sf.jasperreports.engine.export.JRXlsExporter;
//import net.sf.jasperreports.engine.export.JRXlsExporterParameter;
//import net.sf.jasperreports.engine.util.JRLoader;
import net.sf.jasperreports.view.JasperViewer;

import mudbconnection.DBPhysicalSimpleSummary;
import mudbconnection.DBQuestionnaireSimpleSummary;
import mudbconnection.DBStudentSummary;
import muobject.*;

@Component("PreviewReportService")
public class PreviewReportService implements PreviewReport {
	
	@Autowired
	private ReportGenerator  reportGenerator;
	@Autowired
	private AccessSchools  getSchools;
	@Autowired
	private DBPhysicalSimpleSummary  getPhysicalSimpleSummary;
	@Autowired
	private DBQuestionnaireSimpleSummary  getQuestionnaireSimpleSummary;
	@Autowired
	private DBStudentSummary  getStudentSummary;
	
	static String questionFileName="report/Question.jrxml";
	static String physicalSimpleSummary="report/PhysicalSimpleSummary.jrxml";
	static String questionnaireSimpleSummary="report/QuestionnaireSimpleSummary.jrxml";
	static String studentSummary="report/StudentSummary.jrxml";
	static String PhysicalSexChart="report/PhysicalSexChart.jrxml";
	static String OtherSexChart="report/OtherSexChart.jrxml";
	static String DQuestionnaireSexChart="report/DQuestionnaireSexChart.jrxml";
	static String FQuestionnaireSexChart="report/FQuestionnaireSexChart.jrxml";
	
	public boolean ExistDataTable(){
		boolean booleanPhysicalSimpleSummary = getPhysicalSimpleSummary.ExistDataTable();
		boolean booleanQuestionnaireSimpleSummary = getQuestionnaireSimpleSummary.ExistDataTable();
		boolean booleanStudentSummary = getStudentSummary.ExistDataTable();
		return (booleanPhysicalSimpleSummary == true &  booleanQuestionnaireSimpleSummary == true & booleanStudentSummary == true) ? true : false;
	}
	
	public boolean DataCalaulator(String sno, List<String> years, List<String> grades, List<String> sclasses, List<String> stnos) throws Exception{
        try{
        	List<PhysicalSimpleSummary> physicalSimpleSummarys = reportGenerator.getPhysicalSimpleSummary(sno, years, grades, sclasses, stnos);
        	getPhysicalSimpleSummary.deleteAllPhysicalSimpleSummary();
        	for(PhysicalSimpleSummary physicalSimpleSummary : physicalSimpleSummarys){
        		getPhysicalSimpleSummary.setPhysicalSimpleSummary(physicalSimpleSummary);}
        	
        	List<QuestionnaireSimpleSummary> questionnaireSimpleSummarys = reportGenerator.getQuestionnaireSimpleSummary(sno, years, grades, sclasses, stnos);
        	getQuestionnaireSimpleSummary.deleteAllQuestionnaireSimpleSummary();
        	for(QuestionnaireSimpleSummary questionnaireSimpleSummary : questionnaireSimpleSummarys){
        		getQuestionnaireSimpleSummary.setQuestionnaireSimpleSummary(questionnaireSimpleSummary);}
        			
        	List<StudentSummary> studentSummarys = reportGenerator.getStudentSummary(sno, years, grades, sclasses, stnos);
        	getStudentSummary.deleteAllStudentSummary();
        	for(StudentSummary studentSummary : studentSummarys){
        		getStudentSummary.setStudentSummary(studentSummary);}	
        	
        }catch(Exception e){
        	System.out.println(e.getMessage());
        	return false;
        }
        return true;
    }

    public boolean QuestionDisplay(List<Question> questions) throws Exception{
    	
    	QuestionReportDataSource database = new QuestionReportDataSource(questions);
    	
        try{
        	String jasperFile=JasperCompileManager.compileReportToFile(questionFileName);
        	String jrprintFile=JasperFillManager.fillReportToFile(jasperFile,null,database);
        	JasperViewer view  = new JasperViewer(jrprintFile, false, false);
        	view.setVisible(true);
        }catch(Exception e){
        	System.out.println(e.getMessage());
        	return false;
        }
        return true;
    }
    
    public boolean PhysicalSampleSummaryDisplay(String sno, List<String> years, List<String> grades, List<String> sclasses, List<String> stnos) throws Exception{
    	
    	PhysicalSimpleReportDataSource database = new PhysicalSimpleReportDataSource(getPhysicalSimpleSummary.getAllPhysicalSimpleSummary());
    	
    	try{
    		Map<String, String> parameters = new TreeMap<String, String>();
        	parameters.put("Sname", getSchools.getSchoolBySno(sno).getCname());
        	String jasperFile=JasperCompileManager.compileReportToFile(physicalSimpleSummary);
        	String jrprintFile=JasperFillManager.fillReportToFile(jasperFile, parameters, database);
        	JasperViewer view  = new JasperViewer(jrprintFile, false, false);
        	view.setVisible(true);
        }catch(Exception e){
        	System.out.println(e.getMessage());
        	return false;
        }
        return true;
    }
    
    public boolean QuestionnaireSampleSummaryDisplay(String sno, List<String> years, List<String> grades, List<String> sclasses, List<String> stnos) throws Exception{
    	
    	QuestionnaireSimpleReportDataSource database = new QuestionnaireSimpleReportDataSource(getQuestionnaireSimpleSummary.getAllQuestionnaireSimpleSummary());
    	
    	try{
    		Map<String, String> parameters = new TreeMap<String, String>();
        	parameters.put("Sname", getSchools.getSchoolBySno(sno).getCname());
        	String jasperFile=JasperCompileManager.compileReportToFile(questionnaireSimpleSummary);
        	String jrprintFile=JasperFillManager.fillReportToFile(jasperFile, parameters, database);
        	JasperViewer view  = new JasperViewer(jrprintFile, false, false);
        	view.setVisible(true);
        }catch(Exception e){
        	System.out.println(e.getMessage());
        	return false;
        }
        return true;
    }
    
   public boolean StudentSummaryDisplay() throws Exception{
    	
	   StudentSummaryReportDataSource database = new StudentSummaryReportDataSource(getStudentSummary.getAllStudentSummary());
    	
    	try{
    		Map<String, String> parameters = new TreeMap<String, String>();
        	String jasperFile=JasperCompileManager.compileReportToFile(studentSummary);
        	String jrprintFile=JasperFillManager.fillReportToFile(jasperFile, parameters, database);
        	JasperViewer view  = new JasperViewer(jrprintFile, false, false);
        	view.setVisible(true);
        }catch(Exception e){
        	System.out.println(e.getMessage());
        	return false;
        }
        return true;
    }
   
   public boolean PhysicalSexChartDisplay() throws Exception{
   	
	   SexChartDataSource database = new SexChartDataSource(reportGenerator.getPhysicalSexChart());
    	
    	try{
    		List<Double> gripscores = reportGenerator.getPhysicalSexChartTable("gripscore");
    		List<Double> jumpscores = reportGenerator.getPhysicalSexChartTable("jumpscore");
    		List<Double> situpscores = reportGenerator.getPhysicalSexChartTable("situpscore");
    		List<Double> vcapacityscores = reportGenerator.getPhysicalSexChartTable("vcapacityscore");
    		List<Double> run69scores = reportGenerator.getPhysicalSexChartTable("run69score");
    		List<Double> run15scores = reportGenerator.getPhysicalSexChartTable("run15score");
    		
    		Map<String, List> parameters = new TreeMap<String, List>();
    		parameters.put("gripscores", gripscores);
    		parameters.put("jumpscores", jumpscores);
    		parameters.put("situpscores", situpscores);
    		parameters.put("vcapacityscores", vcapacityscores);
    		parameters.put("run69scores", run69scores);
    		parameters.put("run15scores", run15scores);
    		
        	String jasperFile=JasperCompileManager.compileReportToFile(PhysicalSexChart);
        	String jrprintFile=JasperFillManager.fillReportToFile(jasperFile, parameters, database);
        	JasperViewer view  = new JasperViewer(jrprintFile, false, false);
        	view.setVisible(true);
        }catch(Exception e){
        	System.out.println(e.getMessage());
        	return false;
        }
        return true;
    }
   
   public boolean DQuestionnaireSexChartDisplay() throws Exception{
	   	
	   SexChartDataSource database = new SexChartDataSource(reportGenerator.getFQuestionnaireSexChart());
    	
    	try{
    		List<Double> d1scores = reportGenerator.getQuestionnaireSexChartTable("d1score");
    		List<Double> d2scores = reportGenerator.getQuestionnaireSexChartTable("d2score");
    		List<Double> d3scores = reportGenerator.getQuestionnaireSexChartTable("d3score");
    		List<Double> d4scores = reportGenerator.getQuestionnaireSexChartTable("d4score");
    		List<Double> d5scores = reportGenerator.getQuestionnaireSexChartTable("d5score");
    		List<Double> d6scores = reportGenerator.getQuestionnaireSexChartTable("d6score");
    		List<Double> d7scores = reportGenerator.getQuestionnaireSexChartTable("d7score");
    		List<Double> d8scores = reportGenerator.getQuestionnaireSexChartTable("d8score");
    		List<Double> d9scores = reportGenerator.getQuestionnaireSexChartTable("d9score");
    		List<Double> d10scores = reportGenerator.getQuestionnaireSexChartTable("d10score");
    		List<Double> d11scores = reportGenerator.getQuestionnaireSexChartTable("d11score");
    		List<Double> d12scores = reportGenerator.getQuestionnaireSexChartTable("d12score");
    		List<Double> d13scores = reportGenerator.getQuestionnaireSexChartTable("d13score");
    		List<Double> d14scores = reportGenerator.getQuestionnaireSexChartTable("d14score");
    		List<Double> d15scores = reportGenerator.getQuestionnaireSexChartTable("d15score");
    		
    		
    		Map<String, List> parameters = new TreeMap<String, List>();
    		parameters.put("d1scores", d1scores);
    		parameters.put("d2scores", d2scores);
    		parameters.put("d3scores", d3scores);
    		parameters.put("d4scores", d4scores);
    		parameters.put("d5scores", d5scores);
    		parameters.put("d6scores", d6scores);
    		parameters.put("d7scores", d7scores);
    		parameters.put("d8scores", d8scores);
    		parameters.put("d9scores", d9scores);
    		parameters.put("d10scores", d10scores);
    		parameters.put("d11scores", d11scores);
    		parameters.put("d12scores", d12scores);
    		parameters.put("d13scores", d13scores);
    		parameters.put("d14scores", d14scores);
    		parameters.put("d15scores", d15scores);
    		
        	String jasperFile=JasperCompileManager.compileReportToFile(DQuestionnaireSexChart);
        	String jrprintFile=JasperFillManager.fillReportToFile(jasperFile, parameters, database);
        	JasperViewer view  = new JasperViewer(jrprintFile, false, false);
        	view.setVisible(true);
        }catch(Exception e){
        	System.out.println(e.getMessage());
        	return false;
        }
        return true;
    }
   
   public boolean FQuestionnaireSexChartDisplay() throws Exception{
	   	
	   SexChartDataSource database = new SexChartDataSource(reportGenerator.getFQuestionnaireSexChart());
    	
    	try{
    		List<Double> f1scores = reportGenerator.getQuestionnaireSexChartTable("f1score");
    		List<Double> f2scores = reportGenerator.getQuestionnaireSexChartTable("f2score");
    		List<Double> f3scores = reportGenerator.getQuestionnaireSexChartTable("f3score");
    		List<Double> f4scores = reportGenerator.getQuestionnaireSexChartTable("f4score");
    		List<Double> f5scores = reportGenerator.getQuestionnaireSexChartTable("f5score");
    		List<Double> f6scores = reportGenerator.getQuestionnaireSexChartTable("f6score");
    		List<Double> f7scores = reportGenerator.getQuestionnaireSexChartTable("f7score");
    		List<Double> f8scores = reportGenerator.getQuestionnaireSexChartTable("f8score");
    		List<Double> f9scores = reportGenerator.getQuestionnaireSexChartTable("f9score");
    		List<Double> f10scores = reportGenerator.getQuestionnaireSexChartTable("f10score");
    		
    		Map<String, List> parameters = new TreeMap<String, List>();
    		parameters.put("f1scores", f1scores);
    		parameters.put("f2scores", f2scores);
    		parameters.put("f3scores", f3scores);
    		parameters.put("f4scores", f4scores);
    		parameters.put("f5scores", f5scores);
    		parameters.put("f6scores", f6scores);
    		parameters.put("f7scores", f7scores);
    		parameters.put("f8scores", f8scores);
    		parameters.put("f9scores", f9scores);
    		parameters.put("f10scores", f10scores);
    		
        	String jasperFile=JasperCompileManager.compileReportToFile(FQuestionnaireSexChart);
        	String jrprintFile=JasperFillManager.fillReportToFile(jasperFile, parameters, database);
        	JasperViewer view  = new JasperViewer(jrprintFile, false, false);
        	view.setVisible(true);
        }catch(Exception e){
        	System.out.println(e.getMessage());
        	return false;
        }
        return true;
    }
   
   
   public boolean OtherSexChartDisplay() throws Exception{
	   	
	   SexChartDataSource database = new SexChartDataSource(reportGenerator.getOtherSexChart());
    	
    	try{
    		List<Double> totalscores = reportGenerator.getOtherSexChartTable("totalscore", 0, 30);
    		List<Double> dtotalscores = reportGenerator.getOtherSexChartTable("dtotalscore", 0, 40);
    		List<Double> ftotalscores = reportGenerator.getOtherSexChartTable("ftotalscore", 0, 30);
    		List<Double> BMIs = reportGenerator.getOtherSexChartTable("BMI", 0, 0);
    		List<Double> hwscores = reportGenerator.getOtherSexChartTable("hwscore", 0, 5);
    		List<Double> totals = reportGenerator.getOtherSexChartTable("total", 0, 0);
    		
    		
    		Map<String, List> parameters = new TreeMap<String, List>();
    		
    		parameters.put("totalscores", totalscores);
    		parameters.put("dtotalscores", dtotalscores);
    		parameters.put("ftotalscores", ftotalscores);
    		parameters.put("BMIs", BMIs);
    		parameters.put("hwscores", hwscores);
    		parameters.put("totals", totals);
    		
        	String jasperFile=JasperCompileManager.compileReportToFile(OtherSexChart);
        	String jrprintFile=JasperFillManager.fillReportToFile(jasperFile, parameters, database);
        	JasperViewer view  = new JasperViewer(jrprintFile, false, false);
        	view.setVisible(true);
        }catch(Exception e){
        	System.out.println(e.getMessage());
        	return false;
        }
        return true;
    }
    
    /*public void Display() throws Exception{
    	
    	//File f = new File(".");
    	//System.out.println("Current dir : " + f.getCanonicalPath());
    	List<Object> questions = new ArrayList<Object>();
    	questions.add(new  Question(1, "Q1", "2012", "中文", "", null, null));
    	//schools.add(new School(2,"CDSJ6","north"));
    	//QuestionReportDataSource database = new QuestionReportDataSource(questions);
    	
        long startTime=System.currentTimeMillis();
        
        String jasperFile=JasperCompileManager.compileReportToFile(questionFileName);
        
        //String jrprintFile=JasperFillManager.fillReportToFile(jasperFile,null,database);
        //JasperExportManager.exportReportToHtmlFile(jrprintFile);
        
        //JasperExportManager.exportReportToPdfFile(jrprintFile);
        
        //JasperExportManager.exportReportToXmlFile(jrprintFile,false);
        try{
        	//JasperViewer view  = new JasperViewer(jrprintFile, false, false);
        	//view.setVisible(true);
        	//JasperViewer.viewReport(jrprintFile, false);
        }catch(Exception e){
        	
        }
        
        
        //File sourceFile = new File(jrprintFile);
        //JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject(sourceFile);
        //File destFile = new File(sourceFile.getParent(), jasperPrint.getName() + ".xls");
        //JRXlsExporter exporter = new JRXlsExporter();
        //exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
        //exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFile.toString());
        //exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.TRUE);
        //exporter.exportReport();
        long endTime=System.currentTimeMillis();
        long time=(endTime-startTime)/1000;
        System.out.println("success with "+time+" s");
    }*/
}
